using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using MINERP.COM;


namespace MINERP.DAL.PrintManage
{
    public class DALLogin
    {
        public static bool isProprity = false;
        public static bool isRePrint = false;
        public static bool isReLoad = false;
        public static bool isDeleteBoard = false;

        public int GetPrint(out DataSet dsPrint)
        {
            int iRet = 1;

            Database db = null;
            dsPrint = null;

            try
            {
                db = DatabaseFactory.CreateDatabase();
                //string sqlCommand = " select * from printer where status2= " + (int)PrinterStatus.Idle;
                string sqlCommand = " select * from printer";
                DbCommand dbCommand = db.GetSqlStringCommand(sqlCommand);
                dsPrint = db.ExecuteDataSet(dbCommand);
            }
            catch
            {
                iRet = -1;
                return iRet;
            }

            if (dsPrint.Tables[0].Rows.Count == 0)
            {
                iRet = -2;
                return iRet;
            }
            return iRet;
        }

        public int Check(string userName, string pass)
        {
            Database db = null;
            DataSet ds = new DataSet();
            db = DatabaseFactory.CreateDatabase();
            string sqlCommand = "CheckUser";
            DbCommand dbCommand = db.GetStoredProcCommand(sqlCommand);
            db.AddInParameter(dbCommand, "loginname", DbType.String, userName);
            db.AddInParameter(dbCommand, "loginpwd", DbType.String, pass);
            ds = db.ExecuteDataSet(dbCommand);

            if (ds.Tables.Count > 0)
            {
                this.SetControl(userName, pass);

                return (int)ds.Tables[0].Rows[0][0];
            }

            return 0;
        }


        private List<int> SelectOperid(string userName, string pass)
        {
            List<int> list = new List<int>();

            Database db = null;
            DataSet ds = new DataSet();
            DataTable dt = null;

            db = DatabaseFactory.CreateDatabase();
            string sqlCommand = "selectPower";
            DbCommand dbCommand = db.GetStoredProcCommand(sqlCommand);
            db.AddInParameter(dbCommand, "loginname", DbType.String, userName);
            db.AddInParameter(dbCommand, "loginpwd", DbType.String, pass);
            ds = db.ExecuteDataSet(dbCommand);

            if (ds.Tables.Count > 0)
            {
                dt = ds.Tables[0];

                foreach (DataRow row in dt.Rows)
                {
                    list.Add((int)row[0]);
                }
            }

           return list;
        }

        private void SetControl(string userName, string pass)
        {
            List<int> list = SelectOperid(userName,pass);

            for (int i = 0; i < list.Count; i++)
                SetControlProprity(list[i]);
        }

        private void SetControlProprity(int opid)
        {
            Database db = null;
            DataSet ds = new DataSet();
            DataTable dt = null;

            db = DatabaseFactory.CreateDatabase();
            string sqlCommand = "SelectControlName";
            DbCommand dbCommand = db.GetStoredProcCommand(sqlCommand);
            db.AddInParameter(dbCommand, "powerid", DbType.Int32, opid);
            ds = db.ExecuteDataSet(dbCommand);

            if (ds.Tables.Count > 0)
            { 
                dt = ds.Tables[0];

                foreach (DataRow row in dt.Rows)
                {
                    switch (row[0].ToString())
                    {
                        case "toolStripbtnDelBoard":
                            isDeleteBoard = true;
                            break;
                        case "toolStripbtnPriority":
                            isProprity = true;
                            break;
                        case "toolStripbtnReBoard":
                            isReLoad = true;
                            break;
                        case "toolStripbtnRePrint":
                            isRePrint = true;
                            break;
                    }
                }
            }
        }

        public bool IsUser(string userName, string pass)
        {
            Database db = null;
            DataSet ds = new DataSet();
            db = DatabaseFactory.CreateDatabase();
            string sqlCommand = "UserExists";
            DbCommand dbCommand = db.GetStoredProcCommand(sqlCommand);
            db.AddInParameter(dbCommand, "loginname", DbType.String, userName);
            db.AddInParameter(dbCommand, "loginpwd", DbType.String, pass);
            ds = db.ExecuteDataSet(dbCommand);

            if (ds.Tables.Count > 0)
            {
                if ((int)ds.Tables[0].Rows[0][0] > 0)
                    return true;
            }
            return false;
        }
        

        public int Login(int printID)
        {
            int iRet = 1;

            Database db = DatabaseFactory.CreateDatabase();

            string sqlCommand = "dbo.Proc_updatePrintStatus2";
            DbCommand printCommand = db.GetStoredProcCommand(sqlCommand);
            db.AddInParameter(printCommand, "PrintID", DbType.Int32, printID);
            db.AddInParameter(printCommand, "Status2", DbType.Int32, 1);

            db.ExecuteNonQuery(printCommand);

            return iRet;
        }

    }
}
